/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Serial driver interface for Linux
 */
#ifndef CLinuxSerialDriver_H_
#define CLinuxSerialDriver_H_
#include "CSerialDriver.h"

/** Serial driver interface for Linux.
 * Easy-to-use implementation of Serial interface for Linux.
 *
 */
class CLinuxSerialDriver:public CSerialDriver{
	public:
		CLinuxSerialDriver();
		~CLinuxSerialDriver();
		/**
		* Open the serial driver
		* Opens the handle and sets the communication parameters
		* @p serialport : The serial port nname = /dev/ttyS0...
		* @p baudrate : the communication flow rate (speed).
		* @p databits : the character size (7,8 bits)
		* @p parity   : 0=none, 1=odd, 2=even
		* @p stopbits : the number of stop bits (1 or 2)
		*
		* @return true in success, false if failure
		*/
		bool Open(const char *serialport, int baudrate,int dataBits,int parity, int stopBits);
		bool Close();
		
		/**
		 * Clears the serial buffer
		 * @return true on success, else false
		 */
		bool Clear();
				
		bool ReadChar(unsigned char &c);
		
		/**
		 * Read the data from the serial. 
		 * The port is selected and then buffer read one character a time until the length is reached or 
		 * the buffer is emptied and the number of retries is exceeded
		 * 
		 * @p *inbuf buffer that stores the input characthers (user must initialize)
		 * @p length the # of bytes of wanted to receive
		 *
		 * @return     -1 on timeout, else the number of bytes read. 
		 */
		int Read(char *inbuf, int length);
		/**
		 * Writes data to serial
		 *
		 * @p data      data to be written
		 * @p length    the length of the data
		 * @return      true in success, else false
		 */
		bool Write(char* data, int length);

		
	private:
		int portHandle;
		/**
		 * Opens serial port 
		 * @p port The serial port folder "/dev/ttyS0" 
		 * @return true if success, else false
		 */
		bool openSerialPortHandle(const char *);
		
		/**
		 * setCommParameters
		 * @p baudrate : the communication flow rate (speed).
		 * @p charsize : the character size (7,8 bits)
		 * @p parity   : 0=none, 1=odd, 2=even
		 * @p stopbits : the number of stop bits (1 or 2)
		 *
		 * @return      0 if the settings succeeded.
		 *              -1 if there was an error.
		 */
		int setCommParameters(int baudrate,int dataBits,int parity, int stopBits );
};

#endif

